import 'package:shared_preferences/shared_preferences.dart';

class AppPreferences {
  // 私有函数构造
  static final shared = AppPreferences._();
  AppPreferences._();

  // 本地存储
  late SharedPreferences _prefs;

  // 键
  static const String _kFirstOpened = 'first_opened';
  static const String _kUserToken = 'user_token';

  // get&set
  bool get isFirstRun => !(_prefs.getBool(_kFirstOpened) ?? false);
  void setFirstOpened() => _prefs.setBool(_kFirstOpened, true);

  String? get userToken => _prefs.getString(_kUserToken);
  set userToken(String? token) {
    if (token == null) {
      _prefs.remove(_kUserToken);
    } else {
      _prefs.setString(_kUserToken, token);
    }
  }

  // 初始化
  Future<void> init() async {
    // 获取本地存储
    _prefs = await SharedPreferences.getInstance();
  }
}
